* Load EBP
* ===============
tempfile temp 
import delimited using ${dirInput}/ebp_csv.csv, clear
gen date2=date(date,"MDY")
format date2 %td
drop date 
gen date_monthly=mofd(date2) 
drop date2
save `temp'

* Load CPI, IP and GS1 from FRED
* =====================================
freduse CPIAUCSL INDPRO GS1, clear
gen date_monthly=mofd(daten)
format date_monthly %tm
drop date daten

* Merge 
* ==========
merge 1:1 date_monthly using `temp', nogen keepusing(ebp)

gen logip=log(INDPRO)
gen logcpi=log(CPIAUCSL)
drop INDPRO CPIAUCSL
rename GS1 gs1

* Merge with MP shocks
* ==========================
merge m:1 date_m using ${dirInput}/hfi_mpshocks_final_monthly, keepusing(ffr3shockTightWeighted) keep(1 3) nogen 

* Date formatting
* =====================
gen date_quarterly=qofd(dofm(date_m))
format date_q %tq
gen date = dofm(date_m)
order date
format date %tdCY-N-D 
tsset date_m

* Sample
* ============
drop if ebp==.

* Export
* ===========
export delimited "${dirTemp}/svar_data_base.csv", replace

* Run interpolation
* ====================

* Data to interpolate
preserve
use date_q logTFP lmarkup_PF_w4_var using ${dirTemp}/tsdata_macromicro_long, clear
keep if date_q >= yq(1980,1) & date_q <= yq(2017,3)
export delimited "${dirTemp}/svar_data_tointerp.csv", replace
restore

shell $matlabPath -nodesktop -r "cd('`c(pwd)'/02_prepare_micromacro_data/functions/'); main_interpol; exit;"

* Merge back to monthly data 
* ==============================

preserve 
tempfile temp
import delimited "${dirTemp}/svar_data_interp.csv", clear
rename date aux 
gen date_monthly=monthly(aux,"YM")
format date_m %tm
drop aux
rename lmarkup_pf_w4_var lmarkup_PF_w4_var_interp
rename logtfp logTFP_interp
save `temp'
restore 

merge 1:1 date_m using `temp'

* Save final data
* ==================
keep if mofd(date) >= ym(1981,9) & mofd(date) <= ym(2017,9)
foreach x of varlist logip logcpi logTFP_interp {
	replace `x'=`x'*100
}
keep date gs1 ebp logip logcpi logTFP_interp lmarkup_PF_w4_var_interp ffr3shockTightWeighted
order date gs1 ebp logip logcpi logTFP_interp lmarkup_PF_w4_var_interp ffr3shockTightWeighted
export delimited "${dirTemp}/svar_data_final.csv", replace
